//
//  TrainMvPlayVcc.m
//  SpiritWalk
//
//  Created by shange1999 on 16/3/15.
//  Copyright © 2016年 王鑫. All rights reserved.
//

#import "TrainMvPlayVcc.h"

@interface TrainMvPlayVcc ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,retain)UITableView *tableviewSelf;
@property(nonatomic,retain)NSMutableArray *nameArr;
@property(nonatomic,retain)NSMutableArray *listArr;

@property(nonatomic,retain)ZFPlayerView *playerView;
@property(nonatomic,retain)ZFPlayerView *playMp3;
@property(nonatomic,retain)UIImageView *backImageView;
@property(nonatomic,retain)UIButton *backButton;
@property(nonatomic,assign)NSInteger indexRowNum;
@end

@implementation TrainMvPlayVcc

-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.nameArr=[NSMutableArray array];
        self.listArr =[NSMutableArray array];
        self.indexRowNum= -1;
          }
    return self;

}
- (void)viewDidLoad {
    [super viewDidLoad];
      [self createData];
      [self createView];
    [self createPlayView];
   [self createImageView];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playMV) name:@"index" object:nil];
}
-(void)createImageView{
    self.backImageView = [UIImageView new];
  
    [self.view addSubview:self.backImageView];
    [self.backImageView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.top.right.equalTo(self.view);

        make.top.equalTo(self.view.mas_top).offset(0);
        make.left.equalTo(self.view.mas_left).offset(0);
        make.height.mas_equalTo(250);//蓝色view的高度为50
        make.width.mas_equalTo(WIDTH);//蓝色view的高度为50
//        make.size.equalTo(CGSizeMake(WIDTH, 250));
        // 注意此处，宽高比16：9优先级比1000低就行，在因为iPhone 4S宽高比不是16：9
//        make.height.equalTo(self.playerView.mas_width).multipliedBy(10.0f/16.0f).with.priority(750);
    }];
    self.backButton =[UIButton buttonWithType:UIButtonTypeCustom];
    [self.backButton setImage:[UIImage imageNamed:@"play_back_full"] forState:UIControlStateNormal];
    self.backButton.frame=CGRectMake(15, 11, 30, 30);
    self.backImageView.userInteractionEnabled = YES;
    [self.backImageView addSubview:self.backButton];
    [self.backButton addTarget:self action:@selector(back:) forControlEvents:UIControlEventTouchUpInside];
    
}
-(void)back:(UIButton*)button{
    NSLog(@"11");
[self dismissViewControllerAnimated:YES completion:^{
}];
}
-(void)createPlayView {
    
    [self.backImageView removeFromSuperview];

        self.playMp3=[ZFPlayerView setupZFPlayer];
        self.playerView = [ZFPlayerView setupZFPlayer];
    
        [self.view addSubview:self.playerView];
        
        [self.playerView mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.left.top.right.equalTo(self.view);
            make.top.equalTo(self.view.mas_top).offset(0);
            make.left.equalTo(self.view.mas_left).offset(0);
             NSString *model = [[UIDevice currentDevice] model];
            NSLog(@"------%@",model);
            if ([model isEqualToString:@"iPhone4,1"]) {
            make.height.mas_equalTo(270);//蓝色view的高度为50
            }else{
              make.height.mas_equalTo(250);//蓝色view的高度为50
            }
            make.width.mas_equalTo(WIDTH);//蓝色view的高度为50
            // 注意此处，宽高比16：9优先级比1000低就行，在因为iPhone 4S宽高比不是16：9
//            make.height.equalTo(self.playerView.mas_width).multipliedBy(10.0f/16.0f).with.priority(750);
        }];
        [self.playerView addSubview:self.backButton];

 
    
}


-(void)createView{
    
    self.tableviewSelf=[[UITableView alloc ]initWithFrame:CGRectMake(0,250, WIDTH, HEIGHT-64) style:0];
    self.tableviewSelf.rowHeight=50;
    [self.view addSubview:self.tableviewSelf];
    self.tableviewSelf.separatorStyle = NO;
    self.tableviewSelf.delegate=self;
    self.tableviewSelf.dataSource=self;
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (self.nameArr.count==0) {
        return 0;
    }else{
        return self.nameArr.count;
    }
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
  
    static NSString * reuse =@"tbvCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse];
    }
    Resolve_listModel  *model =self.nameArr[indexPath.row];
    NSString *miu =[NSString stringWithFormat:@"%d",[model.sum_times intValue]/60];
    NSString *sec =[NSString stringWithFormat:@"%d",[model.sum_times intValue]%60];
    cell.detailTextLabel.text =[NSString stringWithFormat:@"%@分%@秒",miu,sec];
    cell.textLabel.text =model.name;
 
    
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (0==indexPath.section) {
        NSInteger index =indexPath.row;
        NSDictionary *dic =[NSDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"%ld",(long)index],[NSString stringWithFormat:@"%ld",(long)index], nil];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"index" object:nil userInfo:dic];
        
        
    }
}
-(void)playMV{
    NSIndexPath *index =[self.tableviewSelf indexPathForSelectedRow]; //获取当前选中的cell
    [self.playerView removeFromSuperview];
    [self.playMp3 removeFromSuperview];
    NSLog(@"%@",index);
    if (self.listArr.count>0) {
        listModel  *model  = self.listArr[index.row];
        [self createPlayView];
        self.playerView.videoURL = [NSURL URLWithString:model.path];
        self.playMp3.videoURL =[NSURL URLWithString:model.voice];
        
        
    }else{
        NSLog(@"天啊这妈的网络");
    }
}

-(void)createData{
    self.nameArr =[Resolve_listModel baseModelByArr:self.receiveArr];
        NSString *usrStr=@"http://so.jianshen.so/Realtech/Course/selectCourseVideoList";
        NSDictionary *dic = @{@"dayId" :self.dayID,@"userId" : @"9607" };
        [Tool POST:usrStr parameters:dic black:^(id data) {
            NSMutableArray *arr =[listModel baseModelByArr:data[@"data"][@"list"]];
            self.listArr = arr;
            [self.tableviewSelf reloadData];
        }];
}
-(void)setListArr:(NSMutableArray *)listArr{
     NSIndexPath *index =[self.tableviewSelf indexPathForSelectedRow]; //获取当前选中的cell
    if (_listArr!=listArr) {
        _listArr=listArr;
        if (_listArr.count>0) {
            listModel *model =_listArr[index.row];
            [self.backImageView sd_setImageWithURL:[NSURL URLWithString:model.picurl]];
        }else{
            NSLog(@"背景照");
        }
    }
}













- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
